﻿using System.Collections;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace BPM.Web.Shared.Data;

/// <summary>
/// 菜单树（列表形式）
/// </summary>
public class MenuOutput : BaseVM, ITreeNode<MenuOutput, long>
{
    /// <summary>
    /// 主键Id
    /// </summary>
    [DisplayName("业务号")]
    [Required(ErrorMessage = "业务号不能为空")]
    [JsonPropertyName("id")]
    public long Id { get; set; }

    /// <summary>
    /// 子节点
    /// </summary>
    public IEnumerable<MenuOutput> Children { get; set; } = Enumerable.Empty<MenuOutput>();

    /// <summary>
    /// 父Id
    /// </summary>
    [DisplayName("上级菜单")]
    public long Pid { get; set; }

    /// <summary>
    /// 名称
    /// </summary>
    [DisplayName("菜单名称")]
    [Required]
    public string? Name { get; set; }

    /// <summary>
    /// 菜单编号
    /// </summary>
    [DisplayName("菜单编号")]
    [Required]
    public string? Code { get; set; }

    /// <summary>
    /// 菜单类型（字典 0目录 1菜单 2按钮）
    /// </summary>
    [DisplayName("菜单层级")]
    [Required]
    public MenuType Type { get; set; } = MenuType.DIR;

    /// <summary>
    /// 图标
    /// </summary>
    [DisplayName("图标")]
    public string? Icon { get; set; }

    /// <summary>
    /// 路由地址
    /// </summary>
    [DisplayName("路由地址")]
    [Required]
    public string? Router { get; set; }

    private string? _component;
    /// <summary>
    /// 组件地址
    /// </summary>
    [DisplayName("前端组件")]
    [Required]
    public string? Component
    {
        get
        {
            if (Type == MenuType.DIR && (OpenType == MenuOpenType.NONE || OpenType == MenuOpenType.COMPONENT))
            {
                return "PageView";
            }
            else if (OpenType == MenuOpenType.INNER && (Type == MenuType.DIR || Type == MenuType.MENU))
            {
                return "IFrame";
            }
            return _component;
        }
        set { _component = value; }
    }

    /// <summary>
    /// 权限标识
    /// </summary>
    [DisplayName("权限标识")]
    [Required]
    public string? Permission { get; set; }

    /// <summary>
    /// 应用分类（应用编码）
    /// </summary>
    [DisplayName("所属应用")]
    [Required]
    public string? Application { get; set; }

    private MenuOpenType _openType;
    /// <summary>
    /// 打开方式（字典 0无 1组件 2内链 3外链）
    /// </summary>
    [DisplayName("打开方式")]
    [Required]
    public MenuOpenType OpenType
    {
        get
        {
            if (Type == MenuType.BTN)
            {
                return MenuOpenType.NONE;
            }
            return _openType;
        }
        set { _openType = value; }
    }

    /// <summary>
    /// 是否可见（Y-是，N-否）
    /// </summary>
    [DisplayName("是否可见")]
    public string? Visible { get; set; }

    /// <summary>
    /// 链接地址
    /// </summary>
    [DisplayName("链接地址")]
    [Required]
    public string? Link { get; set; }

    /// <summary>
    /// 重定向地址
    /// </summary>
    [DisplayName("定向地址")]
    public string? Redirect { get; set; }

    /// <summary>
    /// 权重（字典 1系统权重 2业务权重）
    /// </summary>
    [DisplayName("权重")]
    public MenuWeight Weight { get; set; }

    /// <summary>
    /// 排序
    /// </summary>
    [DisplayName("排序")]
    public int Sort { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    [DisplayName("备注")]
    public string? Remark { get; set; }

    public bool HasChildren => Children.Any();

    public long GetId() => Id;

    public long GetPid() => Pid;
}
